package demo2;

//牛客网:体操队形
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static int[] a;
    static int[] q;
    static int count = 0,n;
    static boolean[] st;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        a = new int[n+1];
        q = new int[n+1];
        st = new boolean[n+1];
        for(int i = 1; i<n+1; i++) {
            a[i] = in.nextInt();
        }
        dfs(1);
        System.out.println(count);
    }
    public static void dfs(int u) {
        if(u == n+1) {
            if(check()) count++;
            return;
        }
        for(int i = 1; i<=n; i++) {
            if(!st[i]) {
                q[u] = i;
                st[i] = true;
                dfs(u+1);
                st[i] = false;
            }
        }
    }
    public static boolean check() {
        for(int i = 1; i<=n; i++) { //每个人
            if(a[i]!=i) {
                if(q[i] >= q[a[i]]) {
                    return false;
                }
            }
        }
        return true;
    }
}